package com.dragon.flow.service.leave.impl;

import com.dragon.flow.dao.leave.IScreeningDao;
import com.dragon.flow.model.leave.Screening;
import com.dragon.flow.service.flowable.IFlowableProcessInstanceService;
import com.dragon.flow.service.leave.IScreeningService;
import com.dragon.tools.pager.PagerModel;
import com.dragon.tools.pager.Query;
import com.dragon.tools.utils.StringTools;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.UUID;


/**
 * description : 筛查Service实现
 */
@Service
public class ScreeningServiceImpl implements IScreeningService {

	@Autowired
	private IScreeningDao screeningDao;
	@Autowired
	private IFlowableProcessInstanceService flowableProcessInstanceService;

	@Override
	public Screening getScreeningById(String id) throws Exception {
		return StringUtils.isNotBlank(id) ? screeningDao.getScreeningById(id.trim()) : null;
	}

	@Override
	public List<Screening> getScreeningByIds(String ids) throws Exception {
		ids = StringTools.converString(ids);
		return StringUtils.isNotBlank(ids) ? screeningDao.getScreeningByIds(ids) : null;
	}

	@Override
	public List<Screening> getScreeningByIdsList(List<String> ids) throws Exception {
		return CollectionUtils.isNotEmpty(ids) ? screeningDao.getScreeningByIdsList(ids) : null;
	}

	@Override
	public List<Screening> getAll(Screening screening) throws Exception {
		return null != screening ? screeningDao.getAll(screening) : null;
	}

	@Override
	public PagerModel<Screening> getPagerModelByQuery(Screening screening, Query query)
			throws Exception {
		PageHelper.startPage(query.getPageIndex(),query.getPageSize());
		Page<Screening> page = screeningDao.getPagerModelByQuery(screening);
		return new PagerModel<Screening>(page);
	}

	@Override
	public int getByPageCount(Screening screening)throws Exception {
		return (null != screening) ? screeningDao.getByPageCount(screening) : 0;
	}

	@Override
	public void insertScreening(Screening screening) throws Exception {
		if (StringUtils.isBlank(screening.getId())){
			screening.setId(UUID.randomUUID().toString().replace("-",""));
		}
		screening.setCreateTime(new Date());
		screening.setUpdateTime(new Date());
		screeningDao.insertScreening(screening);
	}

	@Override
	public void insertScreeningBatch(List<Screening> screenings) throws Exception {
		for (Screening screening : screenings) {
			if (StringUtils.isBlank(screening.getId())){
				screening.setId(UUID.randomUUID().toString().replace("-",""));
			}
			screening.setCreateTime(new Date());
			screening.setUpdateTime(new Date());
		}
		screeningDao.insertScreeningBatch(screenings);
	}

	@Override
	public void delScreeningById(String id) throws Exception {
		if (StringUtils.isNotBlank(id)) {
			Screening screening = screeningDao.getScreeningById(id);
			screeningDao.delScreeningById(id.trim());
//			flowableProcessInstanceService.deleteProcessInstanceById(screening.getProcessInstanceId());
		}
	}

	@Override
	public void delScreeningByIds(String ids) throws Exception {
		ids = StringTools.converString(ids);
		if(StringUtils.isNotBlank(ids)){
			screeningDao.delScreeningByIds(ids);
		}
	}

	@Override
	public void delScreeningByIdsList(List<String> ids) throws Exception {
		if(CollectionUtils.isNotEmpty(ids)){
			screeningDao.delScreeningByIdsList(ids);
		}
	}

	@Override
	public int updateScreening(Screening screening) throws Exception {
		screening.setUpdateTime(new Date());
		return screeningDao.updateScreening(screening);
	}

	@Override
	public int updateScreeningByIds(String ids,Screening screening) throws Exception {
		screening.setUpdateTime(new Date());
		return screeningDao.updateScreeningByIds(StringTools.converString(ids), screening);
	}

	@Override
	public int updateScreeningByIdsList(List<String> ids,Screening screening) throws Exception {
		screening.setUpdateTime(new Date());
		return screeningDao.updateScreeningByIdsList(ids, screening);
	}

	@Override
	public int updateScreeningList(List<Screening> screenings) throws Exception {
		return screeningDao.updateScreeningList(screenings);
	}



}

